<?php
/*
	用户端公共文件，访问用户端时默认加载
*/
defined('BY_JYA') or exit('error');
//扫码进入的
if($_RQ['fromuser'] && $_RQ['action']=='agent.myshop'){
	$this->session('sao_fromuser',$_RQ['fromuser']);
}
if($_RQ['action']=='agent.login'){
	return '';
}
if($_SESSION['os']=='windows'){
	$this->model('member')->checklogin();
}else{
	cfc('core')->checklogin();
}
$return['pluginset']=$this->plugin_setting('setting');
if($_RQ['is_access'] && $_RQ['action']!='agent.login' && $_RQ['action']!='agent.apply' && $_RQ['action'] !='agent.myshop' && $_RQ['action'] !='agent.info'){
	$member=$this->model('member')->checkmember('',true);
}elseif($_RQ['action'] !='agent.myshop' && $_RQ['action']!='agent.apply' && $_RQ['action']!='agent.login'){
	$member=$this->model('member')->checkmember('',false);
}
if($_SESSION['container']=='wxapp' && !$_SESSION['fromh5wxapp']){
	$iswxapp=1;
}
if(($_RQ['is_access'] || $iswxapp) && (!$member['is_agent'] || $member['stop']) && $_RQ['action'] !='agent.myshop' && $_RQ['action'] !='agent.apply'){
	//还不是分销商，跳转申请页
	$return['applystatus']=$this->getcolumn('agent_apply',array('mid'=>$_SESSION['uid']),'status');
	if($return['applystatus']==1 && $member['is_agent']!=1){
		pdo_delete('jy_mjicai_agent_apply',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	}
	$apply=$this->get('agent_apply',array('mid'=>$_SESSION['uid']),array('id','remark'));
	if($apply['id']){
		$return['apply']=$apply;
		$return['applystatus']=$this->getcolumn('agent_apply',array('mid'=>$_SESSION['uid']),'status');
	}
	$apply=$this->getcolumn('agent_apply',array('mid'=>$_SESSION['uid']),'id');
	//判断是否满足申请分销商条件
	$can_apply=true;
	if($this->setting['become']==2){
				//检查消费次数
				$num=pdo_count('jy_weishop_order',array('mid'=>$member['mid'],'status'=>3));
				if($num<$this->setting['num']){
					$can_apply=false;
					$return['now']=$num;
					$return['limit']=$this->setting['num'];
					$return['unit']='次';
				}
	}
	if($this->setting['become']==3){
				//检查消费金额
				$money=pdo_getsum('jy_weishop_order',array('mid'=>$member['mid'],'status'=>3),'price');
				if($money<$this->setting['money']){
					$can_apply=false;
					$return['now']=price($money);
					$return['limit']=price($this->setting['money']);
					$return['unit']='元';
				}
	}
	if($this->setting['become']==4){
				//检查是否购买本商品
				$can_apply=false;
				$become_goods=$this->setting['become_goods'];
				foreach($become_goods as &$bgv){
					$isbuy=pdo_count('jy_weishop_order_goods',array('mid'=>$member['mid'],'goodsid'=>$bgv['id'],'ispay'=>1),'id');
					if($isbuy>0){
						$can_apply=true;
					}
					$goods=pdo_get('jy_weishop_goods',array('id'=>$bgv['id']),array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit'));
					$bgv=array_merge($goods,$bgv);
				}
				$return['become_goods']=$become_goods;
	}
	$return['can_apply']=$can_apply;	
	$return['member']=$member;
	if($member['fromuser']){
		$return['agent']=model('core/member')->detail($member['fromuser'],array('nickname','avatar'));
		$return['agent_name']=$return['agent']['nickname'];
	}
	$return['htmls']=$this->template_url('agent.apply');
	$setting=$this->plugin_setting('page_setting');
	set_html('title',$setting['tcenter']);
	$return['setting']=$setting;
	$return['item']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['item']['diyformfields']=$setting['diyformfields']?:array();
	if($iswxapp){		
		exi('','','jy_mjicai/agent.apply/');
	}
	exi($return);
}


